package cz.data.domain.visual.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.base.TableDataResponse;
import cz.data.common.core.DataConstant;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.visual.mapstruct.ChartMapper;
import cz.data.domain.visual.model.dto.ChartConfig;
import cz.data.domain.visual.model.dto.ChartDto;
import cz.data.domain.visual.model.entity.ChartEntity;
import cz.data.domain.visual.model.query.ChartQuery;
import cz.data.domain.visual.model.vo.ChartVo;
import cz.data.domain.visual.service.ChartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 可视化图表配置信息表 前端控制器
 * </p>
 */
@Api(tags = {"可视化图表配置"})
@RestController
@RequestMapping("/charts")
public class ChartController extends BaseController {

    @Autowired
    private ChartService chartService;

    @Autowired
    private ChartMapper chartMapper;

    @ApiOperation(value = "可视化图表配置-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<ChartVo> getChartById(@PathVariable String id) {
        ChartEntity chartEntity = chartService.getChartById(id);
        return new SimpleDataResponse<>(chartMapper.toVO(chartEntity));
    }

    @ApiOperation(value = "可视化图表配置-获取列表")
    @GetMapping("/list")
    public TableDataResponse<ChartVo> getChartList() {
        QueryWrapper<ChartEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", DataConstant.EnableState.ENABLE.getKey());
        queryWrapper.select("id", "chart_name");
        List<ChartEntity> list = chartService.list(queryWrapper);
        List<ChartVo> collect = list.stream().map(chartMapper::toVO).collect(Collectors.toList());
        return new TableDataResponse<>(collect);
    }

    @ApiOperation(value = "可视化图表配置-分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chartQuery", value = "查询实体chartQuery", required = true, dataTypeClass = ChartQuery.class)
    })
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<ChartVo>> getChartPage(ChartQuery chartQuery) {
        QueryWrapper<ChartEntity> queryWrapper = new QueryWrapper<>();
        IPage<ChartEntity> page = chartService.page(new Page<>(chartQuery.getPageNum(), chartQuery.getPageSize()), queryWrapper);
        List<ChartVo> collect = page.getRecords().stream().map(chartMapper::toVO).collect(Collectors.toList());
        JsonPage<ChartVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperation(value = "可视化图表配置-添加信息")
    @ApiImplicitParam(name = "chart", value = "详细实体chart", required = true, dataType = "ChartDto")
    @PostMapping()
    public SimpleDataResponse<ChartVo> saveChart(@RequestBody @Validated({ValidationGroups.Insert.class}) ChartDto chart) {
        ChartEntity chartEntity = chartService.saveChart(chart);
        return new SimpleDataResponse<>(chartMapper.toVO(chartEntity));
    }

    @ApiOperation(value = "可视化图表配置-修改信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "chart", value = "详细实体chart", required = true, dataType = "ChartDto")
    })
    @PutMapping("/{id}")
    public SimpleDataResponse<ChartVo> updateChart(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) ChartDto chart) {
        ChartEntity chartEntity = chartService.updateChart(chart);
        return new SimpleDataResponse<>(chartMapper.toVO(chartEntity));
    }

    @ApiOperation(value = "可视化图表配置-删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public BaseResponse deleteChartById(@PathVariable String id) {
        chartService.deleteChartById(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "可视化图表配置-批量删除")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/batch/{ids}")
    public BaseResponse deleteChartBatch(@PathVariable List<String> ids) {
        chartService.deleteChartBatch(ids);
        return BaseResponse.success();
    }

    @PostMapping("/copy/{id}")
    public BaseResponse copyChart(@PathVariable String id) {
        chartService.copyChart(id);
        return BaseResponse.success();
    }

    @PutMapping("/build/{id}")
    public BaseResponse buildChart(@PathVariable String id, @RequestBody ChartDto chart) {
        chartService.buildChart(chart);
        return BaseResponse.success();
    }

    @PostMapping("/data/parser")
    public SimpleDataResponse<Map<String, Object>> dataParser(@RequestBody @Validated ChartConfig chartConfig) {
        Map<String, Object> map = chartService.dataParser(chartConfig);
        return new SimpleDataResponse<>(map);
    }
}
